package com.xdwanj.servlet;

import com.xdwanj.pojo.AdminUser;
import com.xdwanj.service.AdminUserService;
import com.xdwanj.service.impl.AdminUserServiceImpl;
import com.xdwanj.util.ConstantsRes;
import com.xdwanj.util.ConstantsStr;
import com.xdwanj.util.UrlUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/system")
public class SysServlet extends HttpServlet {
    /**
     * 根请求地址
     */
    public static final String REQUEST_MAPPING = "system";

    /**
     * 操作参数
     */
    public static final String METHOD = "method";

    /**
     * 登陆动作
     */
    public static final String LOGIN = "login";
    /**
     * 登出动作
     */
    public static final String LOGOUT = "logout";

    /**
     * 登陆页面
     */
    public static final String LOGIN_PAGE = "loginPage";


    /**
     * 用户名参数
     */
    public static final String ADMIN_USERNAME = "username";
    /**
     * 密码参数
     */
    public static final String ADMIN_PASSWORD = "password";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter(METHOD);

        if (method == null) {
            method = "";
        }

        switch (method) {
            case LOGIN:
                login(req, resp);
                break;
            case LOGOUT:
                logout(req, resp);
                break;
            case LOGIN_PAGE:
            default:
                forwardLoginPage(req, resp);
                break;
        }

    }

    /**
     * 登出操作
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    private void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String url = null;
        req.getSession().removeAttribute(ConstantsStr.ADMIN_USER_SESSION);

        url = req.getContextPath() + UrlUtil.getUrl(SysServlet.REQUEST_MAPPING, new String[]{METHOD, LOGIN_PAGE});
        System.out.println("url = " + url);
        resp.sendRedirect(url);
    }

    /**
     * 转发到登陆页面
     *
     * @param req
     * @param resp
     */
    private void forwardLoginPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String url = null;

        url = ConstantsRes.SYSTEM_LOGIN_PAGE;
        System.out.println("url = " + url);
        req.getRequestDispatcher(url).forward(req, resp);
    }

    /**
     * 管理者登陆请求
     *
     * @param req
     * @param resp
     */
    private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AdminUserService adminUserService = new AdminUserServiceImpl();
        String url = null;

        String username = req.getParameter(ADMIN_USERNAME);
        String password = req.getParameter(ADMIN_PASSWORD);

        AdminUser user = adminUserService.login(username, password);

        if (user == null) {
            req.setAttribute(ConstantsStr.STATUS, "用户名或密码错误");
            url = UrlUtil.getUrl(SysServlet.REQUEST_MAPPING, new String[]{METHOD, LOGIN_PAGE});
            System.out.println("url = " + url);
            req.getRequestDispatcher(url).forward(req, resp);
            return;
        }

        req.getSession().setAttribute(ConstantsStr.ADMIN_USER_SESSION, user);

        url = req.getContextPath() + "/admin";
        System.out.println("url = " + url);
        resp.sendRedirect(url);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
